<?php

namespace Biz;

use Base\BaseOrmModel;
use Base\Exception\NoRecordException;

class User extends \Base\BaseBiz
{
    public function getUserList($where, $now, $size)
    {
        // 获取客户列表
        $user_list = \Biz\User\User::getList($where, $now, $size);
        $user_account = new \Orm\UserAccount\UserAccountModel();
        $user_list_cls = array();
        $user_list_cls['total'] = $user_list['total'];
        foreach ($user_list['lists'] as $key => $value){
            try {
                $user_account_cls = $user_account->getUserAccountByUid($value['id']);
            }catch (\Exception $e) {
                $user_account_cls = array();
            }
            $user_list_cls['list'][$key]['id'] = $value['id'];
            $user_list_cls['list'][$key]['nickname'] = $value['nickname'];
            $user_list_cls['list'][$key]['headimgurl'] = $value['headimgurl'];
            $user_list_cls['list'][$key]['score'] = !empty($user_account_cls) ? $user_account_cls['score'] : '';
            $user_list_cls['list'][$key]['play_times'] = !empty($user_account_cls) ? $user_account_cls['play_times']:'';
            $user_list_cls['list'][$key]['other_chance'] = !empty($user_account_cls) ?$user_account_cls['other_chance'] :'';
            $user_list_cls['list'][$key]['sex'] = $value['sex'];
            $user_list_cls['list'][$key]['insert_time'] = $value['insert_time'];
        }
        return $user_list_cls;
    }

    /**
     * 获取信息
     */
    public static function getInfo($id)
    {
        $player_orm = new \Orm\User\UserClass();
        $player_orm->loadById($id);

        return $player_orm;
    }

    /**
     * 获取账户信息
     */
    public static function getAccount($id)
    {
        $user_account = new \Orm\UserAccount\UserAccountModel();
        $account_orm = $user_account->getUserAccountByUid($id);
        return $account_orm;
    }

    /**
     * 更新账户信息
     */
    public static function updateAccount($_orm)
    {
        $user_account = new \Orm\UserAccount\UserAccountClass();
        $user_account['id'] = $_orm['id'];
        $user_account['uid'] = $_orm['uid']??'';
        $user_account['username'] = $_orm['username']??'';
        $user_account['nickname'] = $_orm['nickname']??'';
        $user_account['score'] = $_orm['score']??0;
        $user_account['one_week_score'] = $_orm['one_week_score']??0;
        $user_account['two_week_score'] = $_orm['two_week_score']??0;
        $user_account['three_week_score'] = $_orm['three_week_score']??0;
        $user_account['four_week_score'] = $_orm['four_week_score']??0;
        $user_account['play_times'] = $_orm['play_times']??0;
        $user_account['other_chance'] = $_orm['other_chance']??0;

        $is_update = $user_account->update();
        if($is_update){
            return true;
        }else{
            return false;
        }
    }

    public static function insertScoreLog($_orm)
    {
        $user_score_log = new \Orm\UserScoreLog\UserScoreLogClass();
        $user_score_log['uid'] = $_orm['uid']??'';
        $user_score_log['score'] = $_orm['score']??'';
        $user_score_log['play_show_id'] = $_orm['play_show_id']??0;
        $user_score_log['play_target_id'] = $_orm['play_target_id']??0;
        $user_score_log['paly_type'] = $_orm['paly_type']??0;
        $user_score_log['target_title'] = $_orm['target_title']??'';

        $user_score_log->insert();
        if($user_score_log['id']){
            return true;
        }else{
            return false;
        }
    }
}